# -*- coding: utf-8 -*-

import hashlib
import requests
import json
import time
import datetime
import DZnoncestr

API_url = "https://test.dazhii.com/api/blade-api/clue/push"

headers = {
    'Content-Type': "application/json"
}

#字段信息--------------------------------------------------------------------------

#固定信息
tenantId = "000000" #企业ID
channelCode = "37702650"    
cluePoolCode = "76831798"   #指定线索池

#基本信息-表单
name = '导入测试的'
phoneNumber = "15800003311"
city = '深圳'
loanAmount = '100'
age = '30'
sex = '1'
car = '1'
house = '1'
socialInsurance = ''
houseFund = ''
lifeInsurance = ''
profession = ''
salaryMethod = ''
creditCard = ''
particulateLoan = ''

#时间戳
t = time.mktime(datetime.datetime.now().timetuple())
timestamp = str(int(round(t * 1000)))
#6位随机验证码
noncestr = DZnoncestr.generate_verification_code()
#签名
signature = tenantId + phoneNumber + str(noncestr) + timestamp
m = hashlib.md5()
m.update(signature.encode(encoding='utf-8'))
signature_md5 = m.hexdigest()

#------------------------------------------------------------------------------


#线索初始化


#线索信息
clue = {
    "tenantId": tenantId,
    "channelCode": channelCode,
    "cluePoolCode": cluePoolCode,
    "name": name,
    "phoneNumber": phoneNumber,
    "city": city,
    "loanAmount": loanAmount,
    "age": age,
    "sex": sex,
    "noncestr": noncestr,
    "timestamp": timestamp,
    "signature": signature_md5,
    "exdFields": {
        "car": car,
        "house": house,
        "socialInsurance": socialInsurance,
        "houseFund": houseFund,
        "lifeInsurance": lifeInsurance,
        "profession": profession,
        "salaryMethod": salaryMethod,
        "creditCard": creditCard,
        "particulateLoan": particulateLoan
    }
}

#
bodys = json.dumps(clue)

print(bodys)

# requests.DEFAULT_RETRIES = 5  # 增加重试连接次数
# s = requests.session()
# s.keep_alive = False  # 关闭多余连接
# r = requests.post(API_url, headers=headers, data=bodys)

# print(r.text)
